package com.gulimall.auth.controller;

import com.alibaba.fastjson.TypeReference;
import com.gulimall.auth.feign.MemberFeignService;
import com.gulimall.auth.vo.LoginParamVo;
import com.gulimall.common.utils.R;
import com.gulimall.common.vo.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

/**
 * @author coder_ccy
 * @date 2022/4/2 10:27
 */

@Controller
public class LoginController {

    @Autowired
    private MemberFeignService memberFeignService;


    @GetMapping("/")
    public String login(){


        return "index";
    }

    @PostMapping("/auth/login")
    public String doLogin(LoginParamVo loginParamVo, HttpSession session){


        R r = memberFeignService.login(loginParamVo);

        if(r.getCode()==0){

            LoginInfo loginInfo = r.getData(new TypeReference<LoginInfo>() {
            });

            session.setAttribute("loginInfo",loginInfo);



            return "redirect:http://gulimall.com";
        }else{


            return "redirect:http://auth.gulimall.com";
        }

    }

    public static void main(String[] args) {
        // 可逆算法【salt】：bcrypt   123  ==  sdflasf;a;f;sdafja  ==  123   dsfdsafdsafasfa
        // 不可逆的 md5   123 = dfakdsfhasdf   dfakdsfhasdf

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode("123");
        System.out.println(encode);

        System.out.println(passwordEncoder.matches("123", encode));


        String str="123";  // bcrypt 可逆加密算法，对称加密  md5不可逆加密算法  rsa非对称加密
    }
}
